/**
 * Created by Administrator on 2017/6/27.
 */
Tool = {

    "image": function (src) {
        //alert(src);return ;
        var htm2 = "";
        if (src == undefined) {
            return "";
        }
        src = src.split(';');
        for (var i in src) {
            var t_src = src[i];
            if (t_src.length > 1) {
                htm2 = htm2 + "<img src='" + t_src + "' onclick='Tool.big_pic(\" " + t_src + " \")' width='50px'  height='50px'>";
            }
        }
        return htm2;
    },
    // onclick='Tool.new_window(\" " + t_src + " \")'
    "one_image": function (src) {
        var htm2 = "";
        if (src == undefined) {
            return "";
        }
        src = src.split(';');
        for (var i in src) {
            var t_src = src[i];
            if (t_src.length > 1) {
                htm2 = htm2 + "<img src='" + t_src + "' onclick='Tool.new_window(\" " + t_src + " \")' width='50px'  height='50px'>";
                break;
            }
        }
        return htm2;
    },

    "unformat_image": function (cellvalue, options, rowObject) {
        var src = $(rowObject[0]).find("img").attr("src");
        return src;
    },
    "file": function (src) {
        var htm2 = "";
        if (src == undefined) {
            return "";
        }
        src = src.split(';');
        for (var i in src) {
            var t_src = src[i];
            if (t_src.length > 1) {
                htm2 = htm2 + "<button onclick='window.open(\"" + t_src + "\")'>查看附件</button>";
            }
        }
        return htm2;
    },
    "img_unformat": function (cellvalue, options, rowObject) {
        var src = $(rowObject[0]).find("img").attr("src");
        return src;
    },
    "areas": function (area_id) {
        var res = '';
        $.ajax({
            type: "post",
            async: false,
            url: "/admin/tool/areas?area_id=" + area_id,
            success: function (data) {
                res = data.data;
                //console.log(data.data);
            }
        });
        return res;
    },
    "view_info": function (e, a) {
        var index = a.colModel.index;
        var id = a.rowId;
        if (e) {
            return "<button onclick='detail(\"" + id + "\",\"" + index + "\")'>查看详情</button>";
        }
        return '';
    },
    "service_center_view_info": function (e, a) {
        var index = a.colModel.index;
        var id = a.rowId;
        return "<button onclick='detail(\"" + id + "\",\"" + index + "\")'>查看详情</button>";
        return '';
    },
    //商品标准管理中的商品模型Id（等待接口数据）
    "model_id": function (pid) {
        var req = '';
        $.ajax({
            type: "post",
            async: false,
            url: "/admin/goods_model/goods_model_list?pid=" + pid,
            success: function (data) {
                req = data.rows.model_id;
                console.log(data.rows.model_id);
            }
        });
        return req;
    },
    //商品标准管理中的商品模型名称（等待接口数据）
    "model_name": function (pid) {
        var req = '';
        $.ajax({
            type: "post",
            async: false,
            url: "/admin/goods_model/goods_model_list?pid=" + pid,
            success: function (data) {
                req = data.rows.name;
                console.log(data.rows.name);
            }
        });
        return req;
    },
    //商品标准管理中的商品备注（等待接口数据）
    "s_mome": function (pid) {
        var req = '';
        $.ajax({
            type: "post",
            async: false,
            url: "/admin/goods_model/goods_model_list?pid=" + pid,
            success: function (data) {
                req = data.rows.meta_description;
                console.log(data.rows.meta_description);
            }
        });
        return req;
    },
    //商品属性管理中的商品标准id
    "goods_standard_id": function (id) {
        var req = '';
        $.ajax({
            type: "post",
            async: false,
            url: "/admin/tool/goods_standard?standard_id=" + id,
            success: function (data) {
                req = data.data;
            }
        });
        return req;
    },
    //商品属性管理中的模型id
    "goods_model_id": function (id) {
        var reqe = '';
        $.ajax({
            type: "post",
            async: false,
            url: "/admin/tool/goods_model?model_id=" + id,
            success: function (data) {
                reqe = data.data;
            }
        });
        return reqe;
    },
    //商品属性管理中的模型id
    "admin_group": function (id) {
        var reqe = '';
        $.ajax({
            type: "post",
            async: false,
            url: "/admin/tool/admin_group?admin_group_id=" + id,
            success: function (data) {
                reqe = data.data;
            }
        });
        return reqe;
    },
    //ajax提交获取商品名称
    "get_goods_name": function (id) {
        var reqe = '';
        $.ajax({
            type: "post",
            async: false,
            url: "/admin/tool/get_goods_name?goods_id=" + id,
            success: function (data) {
                reqe = data.data;
            }
        });
        return reqe;
    },

    //ajax提交获取商品名称
    "get_sale_time": function (id) {
        var reqe = '';
        $.ajax({
            type: "post",
            async: false,
            url: "/admin/tool/get_sale_time?sale_time_id=" + id,
            success: function (data) {
                reqe = data.data;
            }
        });
        return reqe;
    },

    //ajax提交获取地区
    "get_area_name": function (id) {
        var reqe = '';
        $.ajax({
            type: "post",
            async: false,
            url: "/admin/tool/get_area_name?area_id=" + id,
            success: function (data) {
                reqe = data.data;
            }
        });
        return reqe;
    },


    //商品等级
    "rank": function (area_id) {
        var res = '';
        // $.ajax({
        //     type:"post",
        //     async:false,
        //     url:"/admin/tool/areas?area_id="+area_id,
        //     success:function(data){
        //         res = data.data;
        //         console.log(data.data);
        //         }
        // });
        return res;
    },
    "city": function (city_id) {
        //@ ajax加载位置
        var re = '';
        $.ajax({
            type: "post",
            async: false,
            url: "/admin/tool/city_level?city_id=" + city_id,
            success: function (data) {
                re = data.data;
                //console.log(data.data);
            }
        });
        return re;
    },
    //广告的关联广告位置(未设置路径)
    "ad_ac_id": function (id) {
        var re = '';
        $.ajax({
            type: "post",
            async: false,
            url: "/admin/advertisement_cate_edit?cata=" + id,
            success: function (data) {
                re = data.data;
                console.log(data.data);
            }
        });
        return re;
    },
    "seller_examine_status": function (src) {
        var tmp = [];
        tmp[0] = "未审核";
        tmp[1] = "审核通过";
        tmp[2] = "储备";
        tmp[3] = "淘汰/停用";
        var res = (tmp[src] != undefined) ? tmp[src] : tmp[0];
        return res;
    },
    "seller_commumity_type": function (src) {
        var tmp = [];
        tmp[0] = "社区零售商";
        tmp[1] = "社区批发商";
        var res = (tmp[src] != undefined) ? tmp[src] : tmp[0];
        return res;
    },
    "send_": function (e) {
        var tmp = [];
        tmp[0] = "上午送";
        tmp[1] = "下午送";
        var res = (tmp[e] != undefined) ? tmp[e] : tmp[0];
        return res;
    },
    "goods_examine_status": function (src) {
        var tmp = [];
        tmp[0] = "未审核";
        tmp[1] = "上架";
        tmp[2] = "下架";
        tmp[3] = "滞销";
        tmp[4] = "停用/淘汰";
        tmp[5] = "畅销";
        var res = (tmp[src] != undefined) ? tmp[src] : tmp[0];
        return res;
    },
    "examine_status": function (src) {
        var tmp = [];
        tmp[0] = "未审核";
        tmp[1] = "审核通过";
        tmp[2] = "拒绝";
        tmp[3] = "下架";
        var res = (tmp[src] != undefined) ? tmp[src] : tmp[0];
        return res;
    },
    "is_zonghe": function (src) {
        var tmp = [];
        tmp[100] = "否";
        tmp[101] = "是";
        var res = tmp[src] ? tmp[src] : "否";
        return res;
    },
    "member_type": function (src) {
        var tmp = [];
        tmp[0] = "个人会员";
        tmp[1] = "商户会员";
        var res = (tmp[src] != undefined) ? tmp[src] : tmp[0];
        return res;
    },
    "zitidian_type": function (src) {
        var tmp = [];
        tmp[0] = "<span  style='color:#ff5500' >自提</span>";
        tmp[1] = "<span  style='color: #0e9f5d'>送货上门</span>";
        var res = (tmp[src] != undefined) ? tmp[src] : tmp[0];
        return res;
    },
    "seller_type": function (src) {
        var tmp = [];
        tmp[0] = "<span  style='color: #D43F3A' >后台订单</span>";
        tmp[1] = "<span  style='color: #3B99FC'>即时达</span>";
        var res = (tmp[src] != undefined) ? tmp[src] : tmp[0];
        return res;
    },
    "status": function (src) {
        var tmp = [];
        tmp[0] = "启用";
        tmp[1] = "禁用";
        var res = (tmp[src] != undefined) ? tmp[src] : tmp[0];
        return res;
    },
    "member_type": function (src) {
        var tmp = [];
        tmp[0] = "个人会员";
        tmp[1] = "商户会员";
        var res = (tmp[src] != undefined) ? tmp[src] : tmp[0];
        return res;
    },
    "admin_identity": function (src) {
        var tmp = [];
        tmp[1] = "审核人员";
        tmp[2] = "财务人员";
        tmp[3] = "超级管理员";
        var res = (tmp[src] != undefined) ? tmp[src] : tmp[0];
        return res;
    },
    "time_transformation": function (time) {
        var year = now.getYear();
        var month = now.getMonth() + 1;
        var date = now.getDate();
        var hour = now.getHours();
        var minute = now.getMinutes();
        var second = now.getSeconds();
        return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
    },
    "open_new_window": function (to_url, id) {
        layer.open({
            type: 2,
            title: false,
            offset: '20px',
            area: ['840px', '530px'],
            shadeClose: true,
            closeBtn: 1,
            scrollbar: true,
            cancel: null,
            content: [to_url + id, 'no']
        });
    },
    "new_window": function (a) {
        window.open(a)
    },
    "UnixToDate": function (unixTime, isFull, timeZone) {
        if (typeof (timeZone) == 'number') {
            unixTime = parseInt(unixTime) + parseInt(timeZone) * 60 * 60;
        }
        var time = new Date(unixTime * 1000);
        var ymdhis = "";
        ymdhis += time.getUTCFullYear() + "-";
        ymdhis += (time.getUTCMonth() + 1) + "-";
        ymdhis += time.getUTCDate();
        if (isFull === true) {
            ymdhis += " " + time.getUTCHours() + ":";
            ymdhis += time.getUTCMinutes() + ":";
            ymdhis += time.getUTCSeconds();
        }
        return ymdhis;
    },
    "big_pic": function (src) {
        layer.open({
            title: '详情',
            type: 2,
            offset: '20px',
            area: ['800px', '530px'],
            shadeClose: true,
            closeBtn: 1,
            scrollbar: true,
            cancel: function (index) {
                //location.reload();
                layer.close(index)
            },
            content: '/admin/index/big_image?src=' + src
        });
    }
};
function log_(s) {
    console.log(s);
}


/**
 **img upload
 */

//生成控件，用个hidden来隐藏得到的上传地址，也可以直接获取Img的Src
function ImgUpload(value, editOptions) {
    var span = $("<span>");
    var hiddenValue = $("<input>", {type: "hidden", val: value, name: "fileName", id: "fileName"});
    var image = $("<img>", {name: "uploadimage", id: "uploadimage", style: "display:none; width:150px; height:80px"});
    var el = document.createElement("input");
    el.type = "file";
    el.id = "imgFile_id_no_rename";
    el.name = "file";
    el.onchange = UploadFile;
    span.append(el).append(hiddenValue).append(image);
    return span;
}

function UploadFile() {
    $.ajaxFileUpload
    ({
        url: '/index/tool/upload',
        secureuri: false,
        fileElementId: 'imgFile_id_no_rename',
        dataType: 'json',
        success: function (data, status) {
            if (typeof (data.code) != 'undefined') {
                if (data.code != 0) {
                    // alert(data.code);
                } else {
                    var src = "/uploads/" + data.data;
                    src = encodeURI(src);
                    $("#fileName").val(src);
                    $("#uploadimage").attr("src", src);
                    $("#uploadimage").show();
                    $("#imgFile_id_no_rename").hide();
                }
            }
        },
        error: function (data, status, e) {
            console.log(e);
        }
    })

    return false;
}
function GetImgValue(elem, sg, value) {
    return $(elem).find("#fileName").val();
}

